﻿using System;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Diagnostics;
using System.Configuration;


namespace TapiConnector
{
    public class CreateForm : Form
    {
        Guid contactID;
        Guid accountID;

        private Timer faderTimer = new Timer();
        private Timer displayTimer = new Timer();
        private LinkLabel lblCreateContact;
        private PictureBox pictureBox1;
        private PictureBox pboxClose;
        private volatile bool fadingIn = true;
        private bool draggable = true;
        private bool drag = false;
        private Point start_point = new Point(0, 0);
        private LinkLabel lblName1;
        private static CreateForm instance;

        public delegate void LabelClicked(object sender, EventArgs e);
        public event LabelClicked OnLabelClicked;

        public string MessageTitle {
            get {
                return lblCreateContact.Text;
            }

            set {
                lblCreateContact.Text = value;
            }
        }
       
        public int DisplayInterval
        {
            get
            {
                return displayTimer.Interval / 1000;
            }
            set
            {
                displayTimer.Interval = value * 1000;
            }
        }

        public bool Draggable
        {
            set
            {
                this.draggable = value;
            }
            get
            {
                return this.draggable;
            }
        }
             
        public int FadingInterval {
            get{
                return faderTimer.Interval / 20;
            }
            set{
                faderTimer.Interval = value * 20;
            }
        }

        #region Constructors
        private CreateForm()
        {
            InitializeComponent();

            FadingInterval = 3;
            DisplayInterval = 5;

            faderTimer.Tick += new EventHandler(fader_Tick);
            displayTimer.Tick += new EventHandler(displayTimer_Tick);
            this.MouseDown += new MouseEventHandler(Form_MouseDown);
            this.MouseUp += new MouseEventHandler(Form_MouseUp);
            this.MouseMove += new MouseEventHandler(Form_MouseMove);
        }
        #endregion

        public static CreateForm getInstance() {
            if (instance == null) {
                instance = new CreateForm();
                instance.FillData(String.Empty, String.Empty, String.Empty, String.Empty, String.Empty, Guid.Empty, Guid.Empty);
            }
            return instance;
        }

        public void reset()
        {
            faderTimer.Dispose();
            this.Opacity = 0.0;
        }

        #region FillData
        public void FillData(string cIme, string cPrezime, string cEmail, string cTvrtka, string cTitula, Guid cContactId, Guid cAccountId)
        {

        } 
        #endregion


        #region fadeIn
        public void fadeIn() {
            this.Show();
            fadingIn = true;
            faderTimer.Start();
        }
        #endregion

        #region fadeOut
        public void fadeOut() {
            fadingIn = false;
            faderTimer.Start();
        }
        #endregion

        #region private methods
        #region InitializeComponent
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CreateForm));
            this.lblCreateContact = new System.Windows.Forms.LinkLabel();
            this.lblName1 = new System.Windows.Forms.LinkLabel();
            this.pboxClose = new System.Windows.Forms.PictureBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pboxClose)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // lblCreateContact
            // 
            this.lblCreateContact.AutoSize = true;
            this.lblCreateContact.BackColor = System.Drawing.Color.Transparent;
            this.lblCreateContact.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.lblCreateContact.LinkColor = System.Drawing.Color.DarkBlue;
            this.lblCreateContact.Location = new System.Drawing.Point(62, 61);
            this.lblCreateContact.Name = "lblCreateContact";
            this.lblCreateContact.Size = new System.Drawing.Size(77, 13);
            this.lblCreateContact.TabIndex = 0;
            this.lblCreateContact.TabStop = true;
            this.lblCreateContact.Text = "Create contact";
            this.lblCreateContact.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lbl_Click);
            // 
            // lblName1
            // 
            this.lblName1.AutoSize = true;
            this.lblName1.BackColor = System.Drawing.Color.Transparent;
            this.lblName1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.lblName1.LinkArea = new System.Windows.Forms.LinkArea(0, 0);
            this.lblName1.LinkColor = System.Drawing.Color.Navy;
            this.lblName1.Location = new System.Drawing.Point(62, 15);
            this.lblName1.Name = "lblName1";
            this.lblName1.Size = new System.Drawing.Size(203, 26);
            this.lblName1.TabIndex = 8;
            this.lblName1.Text = "Skype username was not found in CRM.  \r\nCreate a new one?";
            // 
            // pboxClose
            // 
            this.pboxClose.BackColor = System.Drawing.Color.Transparent;
            this.pboxClose.Cursor = System.Windows.Forms.Cursors.Hand;
            this.pboxClose.Image = ((System.Drawing.Image)(resources.GetObject("pboxClose.Image")));
            this.pboxClose.Location = new System.Drawing.Point(329, 13);
            this.pboxClose.Name = "pboxClose";
            this.pboxClose.Size = new System.Drawing.Size(33, 35);
            this.pboxClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pboxClose.TabIndex = 4;
            this.pboxClose.TabStop = false;
            this.pboxClose.Click += new System.EventHandler(this.pictureBox1_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(13, 15);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(32, 32);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            // 
            // CreateForm
            // 
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(239)))), ((int)(((byte)(243)))));
            this.ClientSize = new System.Drawing.Size(370, 138);
            this.Controls.Add(this.lblName1);
            this.Controls.Add(this.pboxClose);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.lblCreateContact);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "CreateForm";
            this.Opacity = 0D;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.Shown += new System.EventHandler(this.CreateForm_Shown);
            this.MouseEnter += new System.EventHandler(this.fadeForm_MouseEnter);
            this.MouseLeave += new System.EventHandler(this.fadeForm_MouseLeave);
            ((System.ComponentModel.ISupportInitialize)(this.pboxClose)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        #region TimerMethods
        private void fader_Tick(object sender, EventArgs e)
        {
            if (fadingIn)
            {
                if (Opacity == 1.0)
                {
                    faderTimer.Dispose();
                    displayTimer.Start();
                }
                else
                {

                    Opacity += 0.02;
                }
            }
            else
            {
                if (Opacity == 0.0)
                {
                    faderTimer.Dispose();
                }
                else {
                    Opacity -= 0.02;
                }
            }
        }

        private void displayTimer_Tick(object sender, EventArgs e)
        {
            displayTimer.Dispose();
            fadeOut();
        }
        #endregion

        #region OnPaintBackground 
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            if (base.DesignMode || base.Visible)
            {
                Rectangle bounds = new Rectangle(0, 0, this.Width, 8);
                using (LinearGradientBrush brush = new LinearGradientBrush(bounds, Color.FromArgb(225, 235, 240), Color.FromArgb(99, 122, 167), LinearGradientMode.Vertical))
                {
                    using (GraphicsPath path = new GraphicsPath())
                    {
                        e.Graphics.FillRectangle(brush, bounds);
                    }
                }

                bounds = new Rectangle(0, 8, this.Width, this.Height - 8);
                using (LinearGradientBrush brush = new LinearGradientBrush(bounds, Color.FromArgb(225, 235, 240), Color.FromArgb(99, 122, 167), LinearGradientMode.ForwardDiagonal))
                {
                    e.Graphics.FillRectangle(brush, bounds);
                }
            }

            using (SolidBrush brushBlack = new SolidBrush(Color.Black))
            {
                using (SolidBrush brushWhite = new SolidBrush(Color.White))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        int x = this.Width / 2 - 25 + i * 5;
                        e.Graphics.FillRectangle(brushWhite, x + 1, 3, 2, 2);
                        e.Graphics.FillRectangle(brushBlack, x, 2, 2, 2);
                    }
                }
            }
        }
        #endregion

        #region Event Handlers

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Visible = false;
            Opacity = 0.0;
        }

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            //
            //On Mouse Down set the flag drag=true and 
            //Store the clicked point to the start_point variable
            //
            this.drag = true;
            this.start_point = new Point(e.X, e.Y);
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            //
            //Set the drag flag = false;
            //
            this.drag = false;
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            //
            //If drag = true, drag the form
            //
            if (this.drag)
            {
                Point p1 = new Point(e.X, e.Y);
                Point p2 = this.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.start_point.X,
                                     p2.Y - this.start_point.Y);
                this.Location = p3;
            }
        }


        private void fadeForm_MouseEnter(object sender, EventArgs e)
        {
            displayTimer.Dispose();
            faderTimer.Dispose();
            Opacity = 1.0;
        }

        private void fadeForm_MouseLeave(object sender, EventArgs e)
        {
            if (!drag)
            {
                displayTimer.Start();
            }
        }

        #endregion



        #endregion

        private void CreateForm_Shown(object sender, EventArgs e)
        {
            this.SuspendLayout();
            this.Visible = true;
            this.StartPosition = FormStartPosition.Manual;
            this.FormBorderStyle = FormBorderStyle.None;

            System.Drawing.Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
            this.Location = new Point(
                   workingRectangle.Width - this.Width, workingRectangle.Height - this.Height);

            this.ResumeLayout();
        }

        private void lbl_Click(object sender, LinkLabelLinkClickedEventArgs e)
        {
                //Uri link = new Uri("http://" + crmServer + ":" + crmServerPort + "/" + crmOrgName + "/main.aspx?etn=contact&pagetype=entityrecord");               
                //Process.Start(link.ToString());


        }


    }

}
